package com.dljd.javase.t13.dom4j;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.FileInputStream;
import java.util.Iterator;
import java.util.List;

public class ParseXML {

    public static void main(String[] args) throws Exception {
        //构造一个解析对象对象

        SAXReader  sax=new SAXReader();

        //使用saxreader对象将磁盘文件中的xml文件读取到内存中的一个Document对象里面

        Document  doc=sax.read(new FileInputStream("resource/xml/test.xml"));

        //读取文档的根元素

        Element  rootEle=doc.getRootElement();

        System.out.println(rootEle.getName());

        Iterator<Element> els= rootEle.elementIterator();

        while(els.hasNext()){
            Element  thisElement=els.next();
            List<Attribute> attrs=thisElement.attributes();
            for(Attribute attr:attrs){
                System.out.println(attr.getName().trim()+"="+attr.getValue().trim());
            }
            System.out.println(thisElement.getName().trim()+"="+thisElement.getText().trim());


            Iterator<Element> iterator = thisElement.elementIterator();
            while(iterator.hasNext()){
                Element  childEle=iterator.next();
                System.out.println(childEle.getName().trim()+"="+childEle.getText().trim());
            }

        }

    }
}
